package com.authine.cloudpivot.ext.service.impl;

import com.authine.cloudpivot.data.support.PageImpl;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.entity.goods.CartEntity;
import com.authine.cloudpivot.ext.service.ICartService;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.cloudpivot.ext.utils.boQuery.BoQueryUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;


public class ICartServiceImpl implements ICartService {

    private ICartServiceImpl() {
    }

    ;

    public static ICartServiceImpl getInstance() {
        return ICartServiceImplBuilder.instance;
    }

    @Override
    public List<CartEntity> listByCartIDs(final List<String> cartIDs) {

        PageImpl<CartEntity> cartEntityPage = BoQueryUtils.Builder(ModelConst.T_CART)
                .condition()
                .eq("deleted", 0)
                .in("id", cartIDs)
                .End().listBO(CartEntity.class);

        return (List<CartEntity>) cartEntityPage.getContent();
    }

    @Override
    public boolean remove(List<String> cartIds) {
        if (CollectionUtils.isEmpty(cartIds)) {
            return false;
        }
        int count = JdbcTemplateUtils.builder(AppConst.COMMODITYMATERIALS, ModelConst.T_CART)
                .in("id", cartIds)
                .deleted();
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean recoverCart(final List<String> cartIds) {
        int count = JdbcTemplateUtils.builder(AppConst.COMMODITYMATERIALS, ModelConst.T_CART)
                .putFieldAndVal("deleted", 0)
                .in("id", cartIds)
                .update();

        return count > 0;
    }

    private static class ICartServiceImplBuilder {
        static final ICartServiceImpl instance = new ICartServiceImpl();
    }
}
